/**
 * Created by George on 2018/3/18.
 */

import util from "../common/util"

// 将本控制器函数作为结果返回给router.js
var createNoteCtrl = function ($scope, $http, $state) {
    $scope.welcome_info = "笔记管理";
    $scope.note = {
        name: "",
        date: util.getNowFormatDate(),
        content: ""
    };
    $scope.$emit('updateRoute', 'note');

    ClassicEditor.create(document.querySelector('#editorContainer'))
        .then(newEditor => {
            $scope.ckEditor = newEditor;
        })
        .catch(error => {
            console.error(error);
        });

    $scope.addNoteClick = function () {
        $scope.note.content = $scope.ckEditor.getData();
        console.log($scope.note);
        var data = {
            "name": $scope.note.name,
            "content": $scope.note.content,
            "date": $scope.note.date
        };
        $http.post('/basketballfire/rest/note', data).then(function (req) {
            console.log("success!");
            alert("创建成功！");
            $state.go('note', {});
        }, function (req) {
            console.log("failed!");
            alert("创建失败！");
        });
    };

    $scope.CancelClick = function () {
        console.log("click cancel");
        $state.go('note', {});
    };

    function init() {
        initOrUpdateData();
    }

    function initOrUpdateData() {
        //TODO
    }


    //app start
    init();
};

export default createNoteCtrl;